import Vue from 'vue';
import VueRouter from 'vue-router';
import Oidc from 'oidc-client'
import userManager from './oidc'

Vue.use(VueRouter);

const routes = [
  {
    path: '/',
    name: 'Index',
    component: () => import('@/views/Index.vue')
  },
  {
    path: '/home',
    name: 'Home',
    component: () => import('@/views/Home.vue')
  },
  {
    path: '/user',
    name: 'User',
    component: () => import('@/views/User/User.vue')
  },
  {
    path: '/callback',
    name: 'Callback',
    component: () => import('@/views/Oidc/Callback.vue')
  },
  {
    path: '/login',
    name: 'Login',
    component: () => import('@/views/Login/Login.vue'),
    meta: {
      noLayout: true
    }
  }
]

const router = new VueRouter({
  mode: 'hash',
  base: process.env.BASE_URL,
  routes
})


router.beforeEach((to, from, next) => {

  //有token或者跳转到回调页面则直接执行
  if (localStorage.getItem('token') || to.path == '/callback') {
    next()
  } else {
    userManager.signinRedirect(); //执行重定向 
  }

  // const token = localStorage.getItem('token');
  // if (!!token) {
  //   if (to.path == '/login') {
  //     next('/index')
  //   } else {
  //     next();
  //   }
  // } else {
  //   if (to.path != '/login') {
  //     next('/login')
  //   } else {
  //     next();
  //   }
  // }
})

export default router;